#!/bin/bash
# Reconfigures an all-in-one image to use a new home domain and number range.
# Usage: reconfigure-aio <home-domain> [<base-number> <number-count>]

# Get command-line arguments.
home_domain=$1
base_number=$2
number_count=$3

if [ -z "$home_domain" ] ; then
  echo "Usage: reconfigure-aio <home-domain> [<base-number> <number-count>]"
fi

# Remove all old numbers from the database, unless they're currently assigned.
# We do this even if the home domain hasn't changed, because the number range might have done (and
# it's hard to tell if that's happened, and cheap/low-impact to just do the reprovisioning).
old_home_domain=$(. /etc/clearwater/config ; echo $home_domain)
echo "DELETE FROM ellis.numbers WHERE number LIKE '%@$old_home_domain' AND owner_id IS NULL ;" | mysql

# Update /etc/clearwater/shared_config, if the home domain has changed.
if [ "$home_domain" != "$old_home_domain" ] ; then
  function escape { echo $1 | sed -e 's/\//\\\//g' ; }
  sed -e 's/^home_domain=.*$/home_domain='$(escape $home_domain)'/g' \
      </etc/clearwater/shared_config >/tmp/shared_config.$$
  mv /tmp/shared_config.$$ /etc/clearwater/shared_config
  
  # Restart clearwater-infrastructure to propagate changes to other configuration files.
  service clearwater-infrastructure restart
fi

# Create new numbers in the new domain, if we've been asked to.
if [ -n "$base_number" ] && [ -n "$number_count" ] ; then
  /usr/share/clearwater/ellis/env/bin/python /usr/share/clearwater/ellis/src/metaswitch/ellis/tools/create_numbers.py --start $base_number --count $number_count
fi

# Restart all the components, if the home domain has changed.
if [ "$home_domain" != "$old_home_domain" ] ; then
  # Work around https://github.com/Metaswitch/sprout/issues/1296.
  service bono stop

  # Restart all the processes.
  for X in /usr/share/clearwater/infrastructure/scripts/restart/* ; do $X ; done

  # Kick monit to wake up and sleep for 10 seconds to make sure it has an accurate view of the system.
  monit
  sleep 10

  # Now wait until all the processes are back up and running (or at least "Uptime failed", which
  # means the process is running, just hasn't been running for very long).
  while monit summary | grep _process | egrep -v "(Running|Uptime failed)" ; do
    echo Some processes still not running - waiting...
    sleep 2
  done
  echo All processes running - configuration complete!
fi
